Basics of using Microsoft (MS) Graph PowerShell to update objects using Hashtables and JSON.

These are just some examples that could be used. By no means would I consider these the “best” way to handle each scenario, however, this should get you started in the right direction.

In general, a good thing to keep in mind, a Microsoft Graph type could be resembled as a PowerShell Hashtable or Array.

To create a PowerShell Hashtable, it would look like this…

$Hashtable = @{}

To create a PowerShell Array, it would look like this…

$Array = @()

I would recommend then converting these data type objects into JSON to attempt to avoid possible data type casting issues.

$json = $Hashtable | ConvertTo-Json -Compress -Depth 99

The –compress parameter removes many of the unnecessary spaces and special characters like new line characters and carriage return characters that might also cause problems.

The -depth parameter ensures the nested Hashtables are also converted.

Scenario: Simple JSON with no complex types

$Properties = @{
     "city" =  "Dallas" # String
     "birthday" = "1970-01-01T00:00:00Z" # DateTime
     "accountEnabled" = $true # Boolean
     "businessPhones" = @("+1 555-555-5551", "+1 555-555-5552") # Collection of strings
     
} | ConvertTo-Json -Compress -Depth 99
 
Update-MgUser -UserId $userid -BodyParameter $Properties

Scenario: What if you wanted to update another complex type for a user? For example the password profile

$Properties = @{
     "city" =  "Dallas" # String
     "birthday" = "1970-01-01T00:00:00Z" # DateTime
     "accountEnabled" = $true # Boolean
     "businessPhones" = @("+1 555-555-5551", "+1 555-555-5552") # List of strings
     "passwordProfile" = @{
       "forceChangePasswordNextSignIn" = $true
       "password" = "new_temp_password"
     }
     
} | ConvertTo-Json -Compress -Depth 99
 
Update-MgUser -UserId $userid -BodyParameter $Properties

Scenario: What if it’s a collection of a complex type? for example the assignedLicenses

$Properties = @{
    "city" =  "Dallas" # String
    "birthday" = "1970-01-01T00:00:00Z" # DateTime
    "accountEnabled" = $true # Boolean
    "businessPhones" = @("+1 555-555-5551", "+1 555-555-5552") # List of strings
    "passwordProfile" = @{
        "forceChangePasswordNextSignIn" = $true
        "password" = "new_temp_password"
     }
     "assignedLicenses" = @(
   @{
     "skuId" = "06ebc4ee-1bb5-47dd-8120-11324bc54e06"
     "disabledPlans" = @("ded3d325-1bdc-453e-8432-5bac26d7a014")
   },
   @{
     "skuId" = "efccb6f7-5641-4e0e-bd10-b4976e1bf68e"
     "disabledPlans" = @("6c57d4b6-3b23-47a5-9bc9-69f17b4947b3")
   }
 
 )
} | ConvertTo-Json -Compress -Depth 99
 
Update-MgUser -UserId $userid -BodyParameter $Properties

Scenario: Or you can just convert a raw JSON string to something usable by MS Graph PowerShell

 
$Json = @'
{
    "city":  "Dallas",
    "birthday": "1970-01-01T00:00:00Z", 
    "accountEnabled": true, 
    "businessPhones": ["+1 555-555-5551", "+1 555-555-5552"],
    "passwordProfile": {
        "forceChangePasswordNextSignIn": true,
        "password": "new_temp_password"
     },
     "assignedLicenses": [
   {
     "skuId": "06ebc4ee-1bb5-47dd-8120-11324bc54e06",
     "disabledPlans": ["ded3d325-1bdc-453e-8432-5bac26d7a014"]
   },
   {
     "skuId": "efccb6f7-5641-4e0e-bd10-b4976e1bf68e",
     "disabledPlans": ["6c57d4b6-3b23-47a5-9bc9-69f17b4947b3"]
   }
 ]
}
'@ | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 99
 
Update-MgUser -UserId $userid -BodyParameter $Json

Scenario: Dynamically add additional properties

Let’s say we start with the following…

$Properties = @{
     "city" =  "Dallas" # String
     "birthday" = "1970-01-01T00:00:00Z" # DateTime
     "accountEnabled" = $true # Boolean
     "businessPhones" = @("+1 555-555-5551", "+1 555-555-5552") # Collection of strings
     
} 

Then later in the script you want to add another property like displayName…

$Properties.displayName = "John Smith"

Or you want to add something like the passwordProfile

$Properties.passwordProfile = @{}
$Properties.passwordProfile.forceChangePasswordNextSignIn = $true
$Properties.passwordProfile.password = "xxx"

Or if you want to add collections like assignedLicenses…

$Properties.assignedLicenses = @()
$Properties.assignedLicenses += @{
     "skuId" = "06ebc4ee-1bb5-47dd-8120-11324bc54e06"
     "disabledPlans" = @("ded3d325-1bdc-453e-8432-5bac26d7a014")
}

Once you’re done adding additional properties, convert it to JSON and send it to MS Graph…

$json = $Properties | ConvertTo-Json -Compress -Depth 99
Update-MgUser -UserId $userid -BodyParameter $Json

More information

Learn more about working with PowerShell HashTables

https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable?view=powershell-7.2

Leave a Comment